<?php

namespace App\library\services;

use App\Models\Live;
use App\Models\LivePlayback;
use App\Models\LiveRecording;
use phpDocumentor\Reflection\Types\Self_;

class LiveRecordingService extends BaseServices
{
    /**
     * Author:Zhongyuan Lu
     * 前台直播列表
     * @param int $page
     * @param int $limit
     * @param int $status
     * @param int $ascription 用户类型
     * @param int $uid
     * @return array
     */
    public static function getLiveList($page, $limit, $status, $ascription, $uid)
    {
        $model = new LiveRecording();
        //直播状态  0  正在直播  1  直播结束
        if (in_array($status,config('setting.live.status'))) {
            $model->where('status',$status);
        }

        if ($ascription == 1) {
            $model->where('uid',$uid);
        }

        $data = $model->orderBy('start_time','desc')->paginate($limit);
        $data && $data = $data->toArray();
        if ($data['total'] > 0){
            foreach ($data['data'] as &$v) {
                $v['start_time'] = date('Y-m-d h:i:s');
            }
            unset($v);
        }

        return self::success('获取成功',$data);
    }

    /**
     * Author:Zhongyuan Lu
     * 录播回调
     * @param $arr
     */
    public static function recordNotify($params)
    {
        $where = [
            ['live_no', '=', $params['channel_id']],
        ];
        //获得直播间id
        $data['live_id'] = Live::getId($where);
        $data['time'] = $params['duration'];           //直播时长
        $data['playback_url'] = $params['video_url'];  //直播回放地址
        $data['day'] = date('Y-m-d',$params['start_time']); //直播日期
        //获得直播记录id和点赞数
        $start_time = strtotime(date('Y-m-d 00:00:00',$params['start_time']));
        $end_time = strtotime(date('Y-m-d 23:59:59',$params['start_time']));

        $liveRecording = LiveRecording::where('live_id',$data['live_id'])->whereBetween('start_time',[$start_time,$end_time])->first();
        $liveRecording && $liveRecording = $liveRecording->toArray();
        if (empty($liveRecording)) {
            $log = ['msg'=>"直播记录未找到",'data'=>$params];
            file_put_contents('liveRecordNotify.log',json_encode($log),FILE_APPEND);
            return false;
        }
        $data['live_recording_id'] = $liveRecording['id'];
        $data['like_num'] = $liveRecording['like_num'];

        $result = LivePlayback::create($data);
        if (empty($result)) {
            $log = ['msg'=>"录制信息添加失败",'liveInfo'=>$params,'playback_info'=>$data];
            file_put_contents('livePlayback.log',json_encode($log),FILE_APPEND);
            return false;
        }
        return true;
    }

    /**
     * Author:Zhongyuan Lu
     * 创建直播记录
     * @param $params
     * @param $uid
     * @return array
     */
    public static function createRecording($params,$uid)
    {
        $live = Live::where('uid',$uid)->first();
        $live && $live = $live->toArray();
        if (empty($live)) {
            return self::fail('暂无开启直播权限，请联系后台管理员');
        }
        $params['live_id'] = $live['id'];
        $params['uid'] = $uid;

        $data = LiveRecording::create($params);
        if (empty($data)) {
            return self::fail('创建失败');
        }

        $url['push_url'] = LiveService::getUrl($live['live_no'],$params['end_time'],1);
        $url['pull_url'] = LiveService::getUrl($live['live_no'],$params['end_time'],2);
        LiveService::saveUrl($url,$live['id']);
        return self::success('创建成功',2);
    }

    /**
     * Author:Zhongyuan Lu
     * 直播详情
     * @param $id
     * @return array
     */
    public static function getLiveEdit($id)
    {
        $liveRecording = LiveRecording::where('id',$id)->select(['live_id','status','is_record'])->first();
        $liveRecording && $liveRecording = $liveRecording->toArray();
        if (empty($liveRecording)) {
            return self::fail('无此记录');
        }

        if ($liveRecording['status'] ==  1) {
            $url = Live::where('id',$liveRecording['live_id'])->select(['push_url','pull_url'])->first();
            return self::success('开始直播',$url,1);
        }else {
            $livePlayback = LivePlayback::where('live_recording_id',$id)->first();
            $livePlayback && $livePlayback = $livePlayback->toArray();

            if (empty($livePlayback)) {
                return self::fail('没有录播');
            }else {
                return self::success('直播回放',$livePlayback,2);
            }
        }
    }
}
